<?php

namespace App\Http\Controllers\Api;
use App\Models\Escort;
use App\Models\SystemConfig;
use App\Models\User;
use App\Models\UserTokens;
use Illuminate\Http\Request;

class Controller
{
    public $user;
    public $type;
    public $account;
    public $uid = 0;
    public $is_escort = 0;
    public $userInfo = null;
    public $platform;  //平台

    public function __construct(Request $request)
    {
        $this->type = $request->route('type');
        $this->platform = strtolower($request->header('platform',''));
        $this->setUserInfo();
    }

    public function setUserInfo()
    {
        $request = request();
        if (empty($request->userInfo)) {
            $authori = \Request::header('Authorization');
            $authori = explode(' ', $authori);
            if(empty($authori) || empty($authori[1])){
                //没token
            }else{
                $access_token = $authori[1];
                $user = [];
                if ($access_token) {
                    $uid = UserTokens::where('access_token', $access_token)->value('uid');
                    $user = User::find($uid);
                    $request->userInfo = $user;
                }
            }
        }


        if ($request->userInfo) {
            $userInfo = array(
                'id' => $request->userInfo->id,
                'avatar' => $request->userInfo->avatar,
                'mobile' => $request->userInfo->mobile,
                'nickname' => $request->userInfo->nickname,
                'realname' => $request->userInfo->realname,
                'state' => $request->userInfo->state,
                'balance' => $request->userInfo->balance,
                'pay_money' => $request->userInfo->pay_money,
                'recharge_money' => $request->userInfo->recharge_money,
                'parent_uid' => $request->userInfo->parent_uid,
            );

            $this->userInfo->is_escort = -1;
            $this->userInfo->is_work=0;

            $escort = Escort::whereUid($userInfo['id'])->first();
            $this->escort_id = $escort->id?:0;
            if($escort && $escort->status == 0){
                $this->userInfo->is_escort = 0;
            }elseif($escort->status > 0){
                $this->userInfo->is_escort = 1;
                $this->userInfo->is_work = $escort->status==1?1:0;
            }
            $userInfo['is_escort'] = $this->userInfo->is_escort;
            $userInfo['is_work'] = $this->userInfo->is_work;
            $this->userInfo = $userInfo;
            $this->uid = (int)$request->userInfo->id;
        }
    }

    /**
     * 成功返回
     * @param string $message
     * @param string $data
     * @return
     */
    public function success($data = '', $message = 'ok')
    {
        $version_config = SystemConfig::whereType(40)->first();//当前所有app版本号

        $platform = strtolower($this->platform);
        $version = '';
        if($platform == 'android'){
            $version = $version_config['config']['android'];
        }elseif($platform == 'wx'){
            $version = $version_config['config']['wxapp_version'];
        }elseif ($platform == 'ios'){
            $version = $version_config['config']['ios_version'];
        }
        response()->json([
            'status' => 200,
            'data' => $data,
            'msg' => $message,
            'time' => time(),
            'version'=>$version
        ], 200, ['charset' => 'utf-8'])->send();
    }

    /**
     * 失败返回
     * $errno
     * @param $status
     * @param string $message
     * @param string $data
     * @return
     */
    public function error($status, $message = '系统错误', $data = '')
    {
        response()->json([
            'status' => $status,
            'data' => $data,
            'msg' => $message,
            'time' => time()
        ], 200, ['charset' => 'utf-8'])->send();
    }

}
